home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.2 / Libraries / IFFParse / Examples / simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  3.4 KB  |  114 lines

  1. /*  :ts=8 bk=0
  2.  *
  3.  * simple.c:    Demonstrates the basic steps required in doing the simplest
  4.  *        possible IFF file parsing.  This program doesn't actually
  5.  *        *do* anything; it just illustrates how the rudimentary stuff
  6.  *        is done.
  7.  *    SYNOPSIS:
  8.  *        simple <filename>
  9.  *
  10.  *        WARNING:  Error checking has been deleted for reasons of
  11.  *        clarity.  Do *not* use this as a model of How To Do It
  12.  *        Properly In The Real World.
  13.  *
  14.  * Leo L. Schwab                8902.23
  15.  * Updated for 1.4 Beta                8912.06
  16.  * Latticeification                9005.30
  17.  */
  18. #include <exec/types.h>
  19. #include <libraries/dosextens.h>
  20. #include <libraries/iffparse.h>
  21. #include <clib/exec_protos.h>
  22. #include <clib/dos_protos.h>
  23. #include "iffparse_protos.h"
  24. #include "iffparse.p"
  25.  
  26.  
  27. struct Library    *IFFParseBase;
  28.  
  29.  
  30. main (argc, argv)
  31. int    argc;
  32. char    **argv;
  33. {
  34.     struct IFFHandle    *iff;
  35.     long            error;
  36.  
  37.     /*
  38.      * Let me say it again.  Error checking has been deleted for clarity.
  39.      * A Real program *MUST* have error checking in it.
  40.      */
  41.     IFFParseBase = OpenLibrary ("iffparse.library", 2L);
  42.  
  43.     /*
  44.      * Allocate the IFFHandle structure.  This is the only supported way
  45.      * to allocate an IFFHandle structure.  Declaring a static structure
  46.      * won't do it, since there are private fields not declared in the
  47.      * iff.h file.  Thus, sizeof (struct IFFHandle), as far as you're
  48.      * concerned, is wrong.  AllocIFF(), however, will get it right
  49.      * every time, in all current and future releases.
  50.      */
  51.     iff = AllocIFF ();
  52.  
  53.     /*
  54.      * Set up the iff_Stream field.  This field is not used by the
  55.      * iffparse.library itself, but is instead passed to the client's
  56.      * read, write, and seek routines, which the library invokes.  This
  57.      * means that iff_Stream can represent anything you wish, since it
  58.      * will be your routines that will interpret it.  In this example,
  59.      * we're opening an AmigaDOS file.
  60.      */
  61.     iff->iff_Stream = Open (argv[1], MODE_OLDFILE);
  62.  
  63.     /*
  64.      * As a convenience, iffparse.library provides call-back routines for
  65.      * direct DOS manipulation internally.  This routine points the
  66.      * IFFHandle's private call-back vectors at those internal routines,
  67.      * saving you the trouble of having to declare them yourself (in your
  68.      * own code space).
  69.      */
  70.     InitIFFasDOS (iff);
  71.  
  72.     /*
  73.      * This "opens" the IFFHandle stream for transaction.  This
  74.      * initializes internal state only; no reading of the file is done.
  75.      */
  76.     OpenIFF (iff, IFFF_READ);
  77.  
  78.     /*
  79.      * This this the biggie.  As used in this example, this routine will
  80.      * trip happily along through the file, verifying syntax, until it
  81.      * hits end-of-file, at which point, it will return the value
  82.      * IFFERR_EOF.
  83.      */
  84.     error = ParseIFF (iff, IFFPARSE_SCAN);
  85.  
  86.     /*
  87.      * If we didn't get IFFERR_EOF back, then something went wrong while
  88.      * scanning the file.  Print out the error code as a diagnostic.
  89.      * (A better approach would be to use the error code as an index into
  90.      * an array of strings, and print the appropriate string.)
  91.      */
  92.     if (error != IFFERR_EOF)
  93.         printf ("Abnormal termination:  error = %ld\n", error);
  94.  
  95.     /*
  96.      * Terminate the transaction with the IFFHandle.  This routine may
  97.      * be called at any time.  All dynamically allocated structures
  98.      * are freed by this call (except the IFFHandle structure itself).
  99.      */
  100.     CloseIFF (iff);
  101.  
  102.     /*
  103.      * Close the I/O stream we opened.  (A DOS file in this example.)
  104.      */
  105.     Close (iff->iff_Stream);
  106.  
  107.     /*
  108.      * Free the IFFHandle structure itself.
  109.      */
  110.     FreeIFF (iff);
  111.  
  112.     CloseLibrary (IFFParseBase);
  113. }
  114.